'use strict'

var path = require('path')
var wx = require('../wx/index')


exports.reply = function* (next) {
	var wechatApi = wx.geWechat()
	var message = this.weixin
	if (message.MsgType === 'event') {
		if (message.Event === 'subscribe') {
			if (message.EventKey) {
				console.log('扫二维码进来：' + message.EventKey +''+ message.ticket)
			}

			this.body = '哈哈，你订阅了找个好'
		}
		else if (message.Event === 'unsubscribe') {
			console.log('无情去官')
			this.body = ''
		}
		else if (message.Event === 'LOCATION') {
			this.body = '您上报的位置：' +message.Latitude + '/' +message.Longitude + '-' + message.Precision
		}
		else if (message.Event === 'CLICK') {
			this.body = '您点击了菜单:' + message.EventKey
		}
		else if (message.Event === 'SCAN' ) {
			console.log('关注后扫二维码' + message.EventKey + ''+ message.Ticket)
			this.body = '看你扫了一下'
		}
		else if (message.Event === 'VIEW') {
			this.body = '你点击了菜单中的链接：' + message.EventKey
		}
		else if (message.Event === 'scancode_push') {
			console.log(message.ScanCodeInfo.ScanType)
			console.log(message.ScanResult.ScanResult)
			this.body = '扫码推送事件：' + message.EventKey
		}
		else if (message.Event === 'scancode_waitmsg') {
			console.log(message.ScanCodeInfo.ScanType)
			console.log(message.ScanCodeInfo.ScanResult)
			this.body = '扫码推送中：' + message.EventKey
		}
		else if (message.Event === 'pic_sysphoto') {
			console.log(message.SendPicsInfo.PicList)
			console.log(message.SendPicsInfo.Count)
			this.body = '弹出系统拍照：' + message.EventKey
		}
		else if (message.Event === 'pic_photo_or_album') {
			console.log(message.SendPicsInfo.PicList)
			console.log(message.SendPicsInfo.Count)
			this.body = '弹出拍照或相册：' + message.EventKey
		}
		else if (message.Event === 'pic_weixin') {
			console.log(message.SendPicsInfo.PicList)
			console.log(message.SendPicsInfo.Count)
			this.body = '弹出拍照或相册：' + message.EventKey
		}

		else if (message.Event === 'location_select') {
			console.log(message.SendPicsInfo.Location_X)
			console.log(message.SendPicsInfo.Location_Y)
			console.log(message.SendPicsInfo.Scale)
			console.log(message.SendPicsInfo.Label)
			console.log(message.SendPicsInfo.Poiname)
			this.body = '弹出拍照或相册：' + message.EventKey
		}
	}
	else if (message.MsgType === 'text') {
		var content = message.Content
		var reply = '额，你说的 ' + message.Content + '太复杂了'

		if (content === '1') {
			reply = '天下第一大傻瓜'
		}
		else if (content === '2') {
			reply = '天下第二大白痴'
		}
		else if (content === '3') {
			reply = '天下第三大白梨'
		}
		else if (content === '4') {
			reply = [{
				title:'倾听你的世界',
				description: '介绍网页',
				picUrl: 'http://cdn.duitang.com/uploads/item/201304/05/20130405122213_xMtaH.jpeg',
				url: 'https://www.ngrok.cc'
			},
			{title:'技术改变的世界',
				description: '天上人间',
				picUrl: 'http://img.bimg.126.net/photo/7iby_IFd0r-KajrDO1aqWw==/309059524445086325.jpg',
				url: 'https://www.ngrok.cc'}
			]
		}
		else if (content === '5') {
			var data = yield wechatApi.uploadMaterial('image',path.join(__dirname, '../2.jpg'))
			reply = {
				type: 'image',
				mediaId: data.media_id
			}
		}
		else if (content === '6') {
			var data = yield wechatApi.uploadMaterial('video',path.join(__dirname, '../6.mp4'))
			reply = {
				type: 'video',
				title: 'asdasdf',
				description:'sadafsd',
				mediaId: data.media_id
			}
		}
		else if (content === '7') {
			var data = yield wechatApi.uploadMaterial('image',path.join(__dirname, '../2.jpg'))
			reply = {
				type: 'music',
				title: 'asdasdf',
				description:'sadafsd',
				musicUrl:'http://mpge.5nd.com/2015-9-12/66325/1.mp3',
				thumbMediaId: data.media_id
			}
		}
		else if (content === '8') {
			var data = yield wechatApi.uploadMaterial('image',path.join(__dirname, '../2.jpg'),{type: 'image'})
			reply = {
				type: 'image',
				mediaId: data.media_id
			}
		}
		else if (content === '9') {
			var data = yield wechatApi.uploadMaterial('video',path.join(__dirname, '../6.mp4'),{type:'video',description:'{"title":"Really a nice place","introduction":"Never think is so easy"}'})
			 console.log("#",data)
			reply = {
				type: 'video',
				title: '烟火',
				description:'saafsd',
				mediaId: data.media_id
			}
		}
		else if (content === '10') {
			var picData = yield wechatApi.uploadMaterial('image',path.join(__dirname, '../2.jpg'),{})
			 var media ={
				articles : [{
					title:'tutut2',
					thumb_media_id: picData.media_id,
					author:'Luff',
					digest:'没有摘要',
					show_cover_pic:1,
					content:'没有内容',
					content_source_url:'https://github.com'
				}]
			}
			data = yield wechatApi.uploadMaterial('news',media,{})
			data = yield wechatApi.fetchMaterial(data.media_id,'news',{ })
			console.log(data)
			var items = data.news_item
			var news = []
			console.log(data)
			items.forEach(function(item){
				news.push({
					title:item.title,
					description:item.digest,
					picUrl:picData.url,
					url:item.url
				})
			})
			reply = news
		}
		else if (content === '11') {
			var counts = yield wechatApi.countMaterial()
			var results = yield [
				wechatApi.batchMaterial({
					type:'image',
					offset:0,
					count:10
				}),
				wechatApi.batchMaterial({
					type:'video',
					offset:0,
					count:10
				}),
				wechatApi.batchMaterial({
					type:'voice',
					offset:0,
					count:10
				}),
				wechatApi.batchMaterial({
					type:'news',
					offset:0,
					count:10
				}),
			]

			console.log(JSON.stringify(results))
			reply = '123'
		}
		else if (content === '12') {
			var group = yield wechatApi.createGroup('wechat2')
				console.log('新建了一个wechat分组')
				console.log(group)	
			var groups = yield wechatApi.fetchGroups()
				console.log('加了 分组列表 wechat')
				console.log(groups)
			var group2 = yield wechatApi.checkGroup(message.FromUserName)
				console.log('查看自己的分组')
				console.log(group2)	
			 reply = 'Group dane!'
		}
		else if (content === '13') {
			var user = wechatApi.fetchUsers(message.FromUserName,'en')
				console.log(user)
			var openIds = [
				{
					openid:message.FromUserName,
					lang:'en'
				}
			]
			var users = yield wechatApi.fetchUsers(openIds)
				console.log(users)
				reply = 'userName'
		}
		else if (content === '14') {
			var userlist = yield wechatApi.listUsers()
			console.log(userlist)
			reply = userlist.total
		}
		else if (content === '15') {
			/*var mpnews = {
				media_id:'jaOmaA0135e578',
			}*/
			var text = {
				content:'hellow wechat'
			}
			var msgData= yield wechatApi.sendBygroup('text',text,'119')
			console.log(msgData)
			reply = 'yes'
		}
		else if (content === '16') {
			var mpnews = {
				media_id:'NgecP12ksiyMHuC3rVuwHDXXwLeEV5_yuY_Tc3odydM',
			}
			/*var text = {
				content:'hellow wechat'
			}*/
			var msgData= yield wechatApi.previewMass('mpnews',mpnews,'119')
			console.log(msgData)
			reply = 'yesid'
		}
		else if (content === '17') {
			/*var mpnews = {
				media_id:'jaOmaA0135e578',
			}*/
			var text = {
				content:'hellow wechat'
			}
			var msgData= yield wechatApi.checkMass('1000000002')
			console.log(msgData)
			reply = '发送成功'
		}
		else if (content === '18') {
			var tempQr = {
				expire_seconds:604800,
				action_name:'QR_SCENE',
				action_info: {
					scene: {
						scene_id: 123
					}
				}
			}
			var  permQr ={
				action_name: 'QR_LIMIT_CENE',
				action_info:{
					scene:{
						scene_str:458
					}
				}
			}
			var permStrQr = {
				action_name: 'QR_LIMIT_STR_SCENE',
				action_info:{
					scene:{
						scene_str:'abc'
					}
				}
			}
			var qr1 = yield wechatApi.createQrcode(tempQr)
			var qr2 = yield wechatApi.createQrcode(permQr)
			var qr3 = yield wechatApi.createQrcode(permStrQr)

			reply = '创建二维码'
		}
		else if (content === '19') {
			var longUrl = 'https://www.imooc.com/'
			var shortData = yield wechatApi.createShorturl(Null,longUrl)
			reply = shortData.short_url 
		}
		else if (content === '20') {
			var semanticData = 
				{
					query:"变形金刚",
					city:"合肥",
					category: "movie",
					uid:"message.FromUserName"
				}
				var _semantic = yield wechatApi.semantic(semanticData)
				console.log(_semantic)
				reply = "天天影院"
		}
		this.body = reply 
	}
	yield next
}